package com.leilei.controller;

import com.leilei.MyObject.Article;
import com.leilei.MyObject.User;
import com.leilei.hibernate.hibernate;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by yvdedu.com on 2016/10/19.
 */
@Controller
public class controller {

    public static hibernate hibernate=new hibernate();

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new MyServlet(), "/ueditor/jsp/controller.jsp");// ServletName默认值为首字母小写，即myServlet
    }


    @RequestMapping("/")
    public String Index(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user==null){
            user=new User();
        }
        List<Article> articleAtTodayUp = hibernate.getArticleAtTodayUp(user.getId(),1);
        model.addAttribute("articles2",articleAtTodayUp);
        model.addAttribute("main",0);
        return "main";
    }

    @RequestMapping("/infocenter")
    public String infocenter(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user==null){
            user=new User();
        }
        List<Article> articlesFromCare = hibernate.getArticlesFromCare(user.getId(),1);
        model.addAttribute("articles",articlesFromCare);
        model.addAttribute("main",1);
        return "infocenter";
    }

    @RequestMapping("/name")
    public String name(Model model){
        String name="This is my name";
        List<Article> articles=hibernate.getArticlesFromCare(0,1);
        if (articles==null)return "name";
        model.addAttribute("name",name);
        model.addAttribute("articles",articles);
        return "name";
    }



    @RequestMapping("/nav")
    public String nav(){
        return "nav";
    }



    @RequestMapping("/Article{id}")
    public String Article(@PathVariable String id,Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user==null){
            user=new User();
        }
        try{
            if (id==null)throw new Exception("无效文章id");
            int Aid=Integer.parseInt(id);
            Article article = hibernate.getArticle(Aid);

            if (article!=null){
                int role = article.getArticleInfo().getRole();
                int role1 = hibernate.getRole(article.getUser().getId(), user.getId());
                if (role1<role){
                    article.setContent("你没有权限查看当前内容！");
                }else if(article.getNoUse()==1){
                    article.setContent("该文章已经被封禁！");
                }else{
                    article.addClick();
                    article= hibernate.updateArticle(article);
                }
                model.addAttribute("article",article);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        model.addAttribute("main",2);
        return "Article";
    }

    //用户页面
    @RequestMapping("/user{id}")
    public String user(@PathVariable String id, Model model, HttpSession session){
        System.out.println(id);
        User user1 = (User) session.getAttribute("user");
        if(user1==null)user1=new User();
        try{
            if (id.isEmpty())throw new Exception("无效用户id");
            int Uid=Integer.parseInt(id);
            if (Uid==user1.getId()) throw new Exception("加载个人主页");
            User user= hibernate.getUser(Uid);
            int role = hibernate.getRole(Uid, user1.getId());
            if (user.getUserInfo().getRole()<=role){
                model.addAttribute("user",user);
                model.addAttribute("articles",hibernate.getArticlesFromUser(user1.getId(),Uid,1));
            }else {
            }
        }catch (Exception e){
            if (user1.getId()!=0){
                model.addAttribute("articles",hibernate.getArticlesFromUser(user1.getId(),1));
                model.addAttribute("role",4);
            }
            e.printStackTrace();
        }
        model.addAttribute("main",2);
        return "user";
    }

    @RequestMapping("/search")
    public String search(String search,String range,String id,HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        if (user==null){
            user=new User();
        }
        if (id==null){
            id= String.valueOf(0);
        }
        int Id= Integer.parseInt(id);
        List<Article> searchArticle=null;
        switch (range){
            case "main":
                searchArticle= controller.hibernate.getSearchArticle(search, 1);
                break;
            case "infocenter":
                searchArticle= controller.hibernate.getSearchArticle(search, user.getId(), 1);
                break;
            case "user":
                searchArticle= controller.hibernate.getSearchArticleFromUser(search, Id, 1);
                break;
        }
        if (hibernate.isManager(user.getId())){
            model.addAttribute("isManager",true);
        }else {
            model.addAttribute("isManager",false);
        }
        model.addAttribute("articles",searchArticle);
        return "search";
    }

    @RequestMapping("/toChangePwd")
    public String changePwd(){
        return "toChangePwd";
    }


}
